package com.impress.commons.vo;


import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonIgnore;

import io.swagger.annotations.ApiModelProperty;

/**
 * 操作的返回类，描述执行是否成功
 * 
 * @author Wangyi
 * @since 2016年6月12日
 */
public class Response implements Serializable {

	private static final long serialVersionUID = -5114142157784526576L;

	private static final String OK_RESPONSE_VALUE = "ok";
	public static final Response OK_RESPONSE = new Response();

	@ApiModelProperty(value = "错误代码, ok表示成功", required = true)
	private String code;

	@ApiModelProperty(value = "用户可读的错误消息")
	private String message;

	@ApiModelProperty(value = "请求ID")
	private String requestId;

	public static Response ok(String message) {
		return new Response(OK_RESPONSE_VALUE, message);
	}

	public static Response error(String code, String message) {
		return new Response(code, message);
	}

	/**
	 * 
	 */
	public Response() {
		this.code = OK_RESPONSE_VALUE;
	}

	/**
	 * @param code
	 * @param message
	 */
	public Response(String code, String message) {
		super();
		if (code == null || code.trim().isEmpty()) {
			throw new NullPointerException("the 'code' argument must not be null or empty");
		}
		this.code = code;
		this.message = message;
	}

	/**
	 * @return 错误码
	 */
	public String getCode() {
		return code;
	}

	/**
	 * 设置返回码
	 * 
	 * @param code
	 */
	public void setCode(String code) {
		if (code == null || code.trim().isEmpty()) {
			throw new NullPointerException("the 'code' argument must not be null or empty");
		}
		this.code = code;
	}

	/**
	 * 获取失败的描述信息
	 * 
	 * @return 错误信息
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * 设置描述信息
	 * 
	 * @param message
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the requestId
	 */
	public String getRequestId() {
		return requestId;
	}

	/**
	 * @param requestId the requestId to set
	 */
	public void setRequestId(String requestId) {
		this.requestId = requestId;
	}

	/**
	 * 是否执行成功
	 * 
	 * @return 是否成功
	 */
	@JsonIgnore
	public boolean isOk() {
		return OK_RESPONSE_VALUE.equals(code);
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("[code=");
		builder.append(code);
		if (message != null && !message.isEmpty()) {
			builder.append(", message=");
			builder.append(message);
		}
		if (requestId != null && !requestId.isEmpty()) {
			builder.append(", requestId=");
			builder.append(requestId);
		}
		builder.append("]");
		return builder.toString();
	}

}
